package com.youlai.boot.modules.banner.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.modules.banner.model.Banner;
import com.youlai.boot.modules.banner.model.BannerForm;
import com.youlai.boot.modules.banner.mapper.BannerMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.youlai.boot.modules.banner.model.BannerVO;
import com.youlai.boot.modules.banner.model.BannerPageQuery;
import lombok.RequiredArgsConstructor;
import com.youlai.boot.modules.banner.converter.BannerConverter;

@Service
@RequiredArgsConstructor
public class BannerService extends ServiceImpl<BannerMapper, Banner> {

    private final BannerMapper bannerMapper;

    @Autowired
    private final BannerConverter bannerConverter;

    public IPage<BannerVO> getBannerPage(BannerPageQuery query) {
        Page<BannerVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        return bannerMapper.getBannerPage(page, query);
    }

    public BannerVO getBannerById(Long id) {
        Banner banner = this.getById(id);
        return bannerConverter.toVO(banner);
    }

    public BannerForm getBannerForm(Long id) {
        Banner banner = this.getById(id);
        return bannerConverter.toForm(banner);
    }

    public Long save(BannerForm form) {
        Banner banner = bannerConverter.toEntity(form);
        bannerMapper.insert(banner);
        return banner.getId();
    }

    public boolean update(BannerForm form) {
        Banner banner = bannerConverter.toEntity(form);
        return bannerMapper.updateById(banner) > 0;
    }

    public boolean delete(Long id) {
        return bannerMapper.deleteById(id) > 0;
    }
}
